package com.hjy.imageloader.test;

import java.util.ArrayList;

import com.hjy.imageloader.ImagesChooseActivity;
import com.hjy.imageloader.ViewHolder;
import com.zhy.imageloader.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

public class ImageListAdapter extends BaseAdapter {

	protected Context mContext;
	protected LayoutInflater mInflater;
	protected ArrayList<String> mdata;

	public ImageListAdapter(Context context, ArrayList<String> data) {
		super();
		mContext = context;
		mInflater = LayoutInflater.from(mContext);
		mdata = data;
		mdata.add("add");// say if add show addimage

	}

	@Override
	public int getCount() {
		return mdata.size();
	}

	@Override
	public Object getItem(int position) {
		return mdata.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		final ViewHolder viewHolder = getViewHolder(position, convertView, parent, R.layout.grid_item);
		this.convert(viewHolder, (String) getItem(position));
		return viewHolder.getConvertView();

	}

	private void convert(final ViewHolder helper, final String item) {
		// ����ͼƬ
		final ImageView mImageView = helper.getView(R.id.id_item_image);
		final ImageView mSelect = helper.getView(R.id.id_item_select);
		mSelect.setVisibility(View.GONE);
		if (item.equals("add")) {
			mImageView.setImageResource(R.drawable.add);
			mImageView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					Intent intent = new Intent(mContext, ImagesChooseActivity.class);
					((Activity) mContext).startActivityForResult(intent, 1);
				}
			});
		} else {
			mImageView.setOnClickListener(null);
			helper.setImageByUrl(R.id.id_item_image, item);

		}
	}

	private ViewHolder getViewHolder(int position, View convertView, ViewGroup parent, int mItemLayoutId) {
		return ViewHolder.get(mContext, convertView, parent, mItemLayoutId, position);
	}

}
